<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="信息内容" prop="content">
        <el-input v-model="queryParams.content" placeholder="请输入信息内容" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="100">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="信息内容" align="center" prop="content">
        <template #default="scope">
          <span @click="showContent(scope.row)">{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接收范围" width="180" show-overflow-tooltip align="center" prop="scope">
        <template #default="scope">
          <dict-tag :options="scopeType" :value="scope.row.scope" />
        </template>
      </el-table-column>
      <el-table-column label="接收人" show-overflow-tooltip align="center" prop="scope">
        <template #default="scope">
          <el-button link type="primary" icon="avatar" @click="handleDetail(scope.row)">接收人</el-button>
        </template>
      </el-table-column>
      <el-table-column label="发送日期" align="center" prop="sendTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.sendTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发送时间" align="center" prop="sendTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.sendTime, "{h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发送单位" align="center" prop="merchantName"></el-table-column>
      <el-table-column label="发送人" align="center" prop="userName"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="[
            {
              label: '正常',
              value: '1'
            },
            {
              label: '禁用',
              value: '0'
            }
          ]" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="updateUserName" />
      <el-table-column label="操作" align="center" class-nafme="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="日期" prop="dateList">
              <el-date-picker v-model="form.dateList" type="dates" range-separator="至" start-placeholder="开始日期"
                value-format="YYYY-MM-DD" end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- :is-range="true"  -->
            <el-form-item label="时间" prop="timeList">
              <el-time-select v-model="form.timeList" placeholder="选择时间">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="范围" prop="scope">
              <el-select v-model="form.scope" placeholder="请选择">
                <el-option v-for="dict in scopeType" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接受人" prop="userIds">
              <el-select v-model="form.userIds" placeholder="请选择" multiple>
                <el-option v-for="dict in rangePeople" :key="dict.value" :label="dict.text"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <el-input v-model="form.content" type="textarea" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <el-dialog title="接收人" v-model="openDetail" append-to-body>
      <el-table :data="gridData">
        <el-table-column property="name" label="姓名" />
        <el-table-column property="avatar" label="头像">
          <template #default="scope">
            <img width="50" height="50" :src="scope.row.avatar" alt="">
          </template>
        </el-table-column>

      </el-table>
    </el-dialog>

  </div>
</template>

<script setup name="Notice">
import {
  informationApiInformationListReceiveUser,
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/backInformation/backInformation";


import { ElMessage, ElMessageBox } from 'element-plus'


const { proxy } = getCurrentInstance();


const scopeType = [
  {
    value: "0",
    label: "今日就诊的患者和监护人",
  },
  {
    value: "1",
    label: "今日治疗的患者和监护人",
  },
  {
    value: "2",
    label: "今日入院的患者和监护人",
  },
  {
    value: "3",
    label: "今日出院的患者和监护人",
  },
  {
    value: "4",
    label: "今日住院的患者和监护人",
  },
  {
    value: "5",
    label: "家庭医生的签约患者和监护人",
  },
  {
    value: "6",
    label: "所有就诊过的患者和监护人",
  },
  {
    value: "7",
    label: "预约就诊的患者和监护人",
  },
  {
    value: "8",
    label: "预约治疗的患者和监护人",
  },
  {
    value: "9",
    label: "预约检查的患者和监护人",
  },
  {
    value: "10",
    label: "所有护士",
  },
  {
    value: "11",
    label: "所有医生",
  },
  {
    value: "12",
    label: "所有签约的家庭医生",
  },
  {
    value: "13",
    label: "所有行政人员",
  },
  {
    value: "14",
    label: "所有后勤人员",
  },
  {
    value: "15",
    label: "所有财务人员",
  },
  {
    value: "16",
    label: "所有采购人员",
  },
  {
    value: "17",
    label: "所有安保员工",
  },
  {
    value: "18",
    label: "所有全体员工",
  },
]



const rangePeople = [
  {
    value: "1741672611994300418",
    text: "陈开权",
    disabled: false,
  },
  {
    value: "1741014236189044737",
    text: "王丽",
    disabled: false,
  },
  {
    value: "1763098108078718978",
    text: "胡三十",
    disabled: false,
  },
  {
    value: "1767071768334483457",
    text: "呼呼呼",
    disabled: false,
  },
]

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const dd = ref('')
const data = reactive({
  form: {
    timeList: ['', '', '1312']
  },
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    content: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
  },
});

const { queryParams, form, rules } = toRefs(data);
const openDetail = ref(false)
const gridData = ref([])
/**详情按钮操作 */
function handleDetail(row) {
  reset();
  const dailyInformationId = row.dailyInformationId || ids.value;
  informationApiInformationListReceiveUser(dailyInformationId).then((response) => {

    gridData.value = response;
    openDetail.value = true;
    title.value = "详情商家";
  });
}

/** 查询信息列表 */
function getList() {
  loading.value = true;
  listNotice(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    dailyInformationId: undefined,
    content: undefined,
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.dailyInformationId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加信息";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const dailyInformationId = row.dailyInformationId || ids.value;
  getNotice(dailyInformationId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "修改信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.dailyInformationId != undefined) {

        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        form.value.timeList = [form.value.timeList]
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const dailyInformationIds = row.dailyInformationId || ids.value;
  proxy.$modal
    .confirm('是否确认删除信息编号为"' + dailyInformationIds + '"的数据项？')
    .then(function () {
      return delNotice(dailyInformationIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

function showContent(row) {
  let content = row.content
  ElMessageBox.alert(content, '信息内容', {
    confirmButtonText: '',
    showConfirmButton: false,
    callback: (action) => {
    },
  })
}

getList();
</script>
